<?php
if(isset($_REQUEST)) extract($_REQUEST);
if(empty($url)){
   die('0'); 
}
if(empty($type)){
   die('1'); 
}
if($type =='ygr.me'){
    $long_url = "http://www.ygr.me/index.php?url=".trim($url);
    $data = file_get_html($long_url);
    $short_url = fetch_match_contents('<td width="32%">
				<a href="', '" target="_blank">',1,$data);
    echo $short_url;
}elseif($type=='url7.me'){
    $long_url = "http://121.199.47.205/web/json.php?type=url7.me&mk=a&uk=a&fdn=&ref=www.ygr.me&xzurl=".trim($url)."&rnd=xz0.4834396476920232";
    $json = file_get_html($long_url);
    $json = str_replace('(', '', $json);
    $json = str_replace(')', '', $json);
    $json = json_decode($json);
    echo $json->url;
}elseif($type=='gh0.cn'){
    $long_url = "http://gh0.cn/index.php?url=".trim($url);
    $data = file_get_html($long_url);
    $short_url = fetch_match_contents('<strong><a href="', '" target="_blank"',1,$data);
    echo $short_url;
}elseif($type=='gurl.cn'){
    $long_url = "http://gurl.cn/api/shorten/?url=".urlencode($url);
    $data = file_get_html($long_url);
    $short_url = fetch_match_contents("shorturl:'", "',longurl",1,$data);
    echo $short_url;
}else{
    exit();
}
//获取远程数据
function file_get_html($url){
    if(function_exists('curl_init')) {
        $ch = curl_init();
        $timeout = 5;
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
        //设置允许curl请求连接的最长秒数，如果设置为0，则无限
        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
        //设置允许curl所有动作执行的最长秒数，如果设置为0，则无限
        curl_setopt ($ch, CURLOPT_TIMEOUT,$timeout*3);
        $contents = curl_exec($ch);
        curl_close($ch);
    } else {
        $contents = file_get_contents($url);
    }
    return $contents;
}
//获取匹配内容
function fetch_match_contents($begin,$end,$num=1,$c)
{
    $begin=change_match_string($begin);
    $end=change_match_string($end);
    if(@preg_match("/{$begin}(.*?){$end}/i",$c,$rs))
    {return $rs[$num];}
    else {return "";}
}//转义正则表达式字符串
function change_match_string($str){
    //注意，以下只是简单转义
    $old=array("/","$");
    $new=array("\/","\$");
    $str=str_replace($old,$new,$str);
    return $str;
}

?>